package com.PMSys.Servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.PMSys.Demain.Department;
import com.PMSys.Demain.Staff;
import com.PMSys.Service.DepartmentService;

public class DepartmentServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public DepartmentServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		// add
		// update
		// delete
		// all
		// part
		String method = request.getParameter("method");

		System.out.println(method);

		if ("add".equals(method)) {
			add(request, response);
		}

		if ("part".equals(method)) {
			part(request, response);
		}

		if ("byid".equals(method)) {
			byid(request, response);
		}

		if ("update".equals(method)) {
			update(request, response);
		}

		if ("delete".equals(method)) {
			delete(request, response);
		}

		if ("emp".equals(method)) {
			emp(request, response);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

	private DepartmentService departmentService;

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		departmentService = new DepartmentService();
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Department department = getBean(request, response);

		if (departmentService.add(department)) {
			// 转发
			all(request, response);
		} else {
			response.getWriter().print("<script>alert('添加失败');window.history.back();</script>");
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Department department = getBean(request, response);

		if (departmentService.update(department)) {
			// 转发
			all(request, response);

		} else {
			response.getWriter().print("<script>alert('更新失败');window.history.back();</script>");
		}

	}

	private void part(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Department department = getBean(request, response);

		List<Department> departmentList = departmentService.selectPart(department);
		request.setAttribute("departmentList", departmentList);

		// 转发
		request.getRequestDispatcher("/Department/select.jsp").forward(request, response);

	}

	private void byid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int idInt = 0;
		String idString = request.getParameter("d_id");
		if (idString != null && !"".equals(idString)) {
			idInt = Integer.parseInt(idString);
		}

		Department department = departmentService.selectById(idInt);
		request.setAttribute("department", department);

		// 转发
		request.getRequestDispatcher("/Department/update.jsp").forward(request, response);

	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int idInt = 0;
		String idString = request.getParameter("d_id");
		if (idString != null && !"".equals(idString)) {
			idInt = Integer.parseInt(idString);
		}

		if (departmentService.delete(idInt)) {
			// 转发
			all(request, response);

		} else {
			response.getWriter().print("<script>alert('删除失败');window.history.back();</script>");
		}

	}

	private void emp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int idInt = 0;
		String idString = request.getParameter("d_id");
		if (idString != null && !"".equals(idString)) {
			idInt = Integer.parseInt(idString);
		}

		List<Staff> staffList = departmentService.selectDepartmentEmployees(idInt);

		if (staffList.size() != 0) {
			request.setAttribute("staffList", staffList);
			// 转发
			request.getRequestDispatcher("/Department/staffList.jsp").forward(request, response);
		} else {
			response.getWriter().print("<script>alert('该部门下无员工');window.history.back();</script>");
		}
	}

	private void all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		List<Department> departmentList = departmentService.selectAll();
		request.setAttribute("departmentList", departmentList);

		// 转发
		request.getRequestDispatcher("/Department/select.jsp").forward(request, response);

	}

	private Department getBean(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Department department = new Department();

		String idString = request.getParameter("d_id");
		if (idString != null && !"".equals(idString)) {
			department.setD_id(Integer.parseInt(idString));
		}

		department.setD_name(request.getParameter("d_name"));

		String typeString = request.getParameter("d_type");
		if (typeString != null && !"".equals(typeString)) {
			department.setD_type(Integer.parseInt(typeString));
		}

		department.setD_phone(request.getParameter("d_phone"));
		department.setD_email(request.getParameter("d_email"));
		department.setD_describe(request.getParameter("d_describe"));

		String superiorIdString = request.getParameter("d_superiorId");
		if (superiorIdString != null && !"".equals(superiorIdString)) {
			department.setD_superiorId(Integer.parseInt(superiorIdString));
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = request.getParameter("d_startDate");
		if (dateString != null && !"".equals(dateString)) {
			try {
				department.setD_startDate(sdf.parse(dateString));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		} else {
			department.setD_startDate(new Date());
		}

		return department;
	}

}
